Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs.Jpeg2000
Imports Vintasoft.Imaging.Codecs

Namespace Imaging.Codecs.Dialogs
	Public Partial Class Jpeg2000EncoderSettingsForm
		Inherits Form

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			formatComboBox.Items.Add(Jpeg2000FileFormat.Jp2File)
			formatComboBox.Items.Add(Jpeg2000FileFormat.Codestream)

			progressionOrderComboBox.Items.Add(ProgressionOrder.CPRL)
			progressionOrderComboBox.Items.Add(ProgressionOrder.LRCP)
			progressionOrderComboBox.Items.Add(ProgressionOrder.PCRL)
			progressionOrderComboBox.Items.Add(ProgressionOrder.RLCP)
			progressionOrderComboBox.Items.Add(ProgressionOrder.RPCL)
		End Sub

		#End Region



		#Region "Properties"

		Private _encoderSettings As Jpeg2000EncoderSettings
		''' <summary>
		''' Gets or sets the JPEG 2000 Encoder Settings.
		''' </summary>
		Public Property EncoderSettings() As Jpeg2000EncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					value = New Jpeg2000EncoderSettings()
				End If
				_encoderSettings = value
				ShowSettings()
			End Set
		End Property

		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)
			If EncoderSettings Is Nothing Then
				EncoderSettings = New Jpeg2000EncoderSettings()
			End If
		End Sub

		Private Sub compressionRatioNumericUpDown_ValueChanged(sender As Object, e As EventArgs)
			compressionRatioLabel.Text = String.Format("(1 : {0})", compressionRatioNumericUpDown.Value)
		End Sub

		Private Sub waveletTransformCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			waveletLevelsNumericUpDown.Enabled = waveletTransformCheckBox.Checked
			If waveletLevelsNumericUpDown.Value = 0 Then
				waveletLevelsNumericUpDown.Value = 5
			End If
		End Sub

		Private Sub ShowSettings()
			formatComboBox.SelectedItem = _encoderSettings.FileFormat
			waveletLevelsNumericUpDown.Value = _encoderSettings.WaveletLevels
			waveletTransformCheckBox.Checked = _encoderSettings.WaveletLevels > 0
			qualityLayersNumericUpDown.Value = _encoderSettings.QualityLayers.Length
			progressionOrderComboBox.SelectedItem = _encoderSettings.ProgressionOrder
			useTilesCheckBox.Checked = _encoderSettings.TileWidth <> 0 AndAlso _encoderSettings.TileHeight <> 0
			lossyCompressionCheckBox.Checked = _encoderSettings.CompressionType = Jpeg2000CompressionType.Lossy
			If useTilesCheckBox.Checked Then
				tileWidthNumericUpDown.Value = Math.Max(tileWidthNumericUpDown.Minimum, _encoderSettings.TileWidth)
				tileHeightNumericUpDown.Value = Math.Max(tileHeightNumericUpDown.Minimum, _encoderSettings.TileHeight)
			End If
			compressionRatioNumericUpDown.Value = CInt(Math.Truncate(Math.Round(_encoderSettings.CompressionRatio)))
			If _encoderSettings.FileSize = 0 Then
				compressionRatioRadioButton.Checked = True
			Else
				imageDataSizeRadioButton.Checked = True
			End If

			UpdateEnabledState()
		End Sub

		Private Sub SetEncoderSettings()
			_encoderSettings.FileFormat = DirectCast(formatComboBox.SelectedItem, Jpeg2000FileFormat)
			If waveletTransformCheckBox.Checked Then
				_encoderSettings.WaveletLevels = CInt(Math.Truncate(waveletLevelsNumericUpDown.Value))
			Else
				_encoderSettings.WaveletLevels = 0
			End If
			_encoderSettings.QualityLayers = New Double(CInt(Math.Truncate(qualityLayersNumericUpDown.Value)) - 1) {}
			For i As Integer = 0 To _encoderSettings.QualityLayers.Length - 1
				_encoderSettings.QualityLayers(i) = 1
			Next
			_encoderSettings.ProgressionOrder = DirectCast(progressionOrderComboBox.SelectedItem, ProgressionOrder)
			If lossyCompressionCheckBox.Checked Then
				_encoderSettings.CompressionType = Jpeg2000CompressionType.Lossy
			Else
				_encoderSettings.CompressionType = Jpeg2000CompressionType.Lossless
			End If
			If useTilesCheckBox.Checked Then
				_encoderSettings.TileWidth = CInt(Math.Truncate(tileWidthNumericUpDown.Value))
				_encoderSettings.TileHeight = CInt(Math.Truncate(tileHeightNumericUpDown.Value))
			Else
				_encoderSettings.TileWidth = 0
				_encoderSettings.TileHeight = 0
			End If
			If _encoderSettings.CompressionType = Jpeg2000CompressionType.Lossy Then
				If imageDataSizeRadioButton.Checked Then
					_encoderSettings.FileSize = CInt(Math.Truncate(imageDataSizeNumericUpDown.Value)) * 1024
				Else
					_encoderSettings.FileSize = 0
					_encoderSettings.CompressionRatio = CDbl(compressionRatioNumericUpDown.Value)
				End If
			End If
		End Sub

		Private Sub UpdateEnabledState(sender As Object, e As EventArgs)
			UpdateEnabledState()
		End Sub


		Private Sub UpdateEnabledState()
			lossyGroupBox.Enabled = lossyCompressionCheckBox.Checked
			imageDataSizeNumericUpDown.Enabled = imageDataSizeRadioButton.Checked
			compressionRatioNumericUpDown.Enabled = compressionRatioRadioButton.Checked
			tileWidthNumericUpDown.Enabled = useTilesCheckBox.Checked
			tileHeightNumericUpDown.Enabled = useTilesCheckBox.Checked
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			Try
				SetEncoderSettings()
				DialogResult = DialogResult.OK
			Catch ex As Exception
				DemosTools.ShowErrorMessage(ex)
			End Try
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		#End Region



	End Class
End Namespace
